<template>
  <div>
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm" class="form">
      <el-form-item prop="date">
        <el-date-picker
            v-model="dataForm.date"
            type="date"
            placeholder="选择日期"
            class="input"
            size="medium"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button size="medium" type="primary" @click="searchHandle()">查询</el-button>
        <el-button size="medium" type="danger" @click="addHandle()">会议申请</el-button>
      </el-form-item>
      <el-form-item class="mold">
        <el-radio-group v-model="dataForm.mold" size="medium" @change="changeHandle">
          <el-radio-button label="全部会议"></el-radio-button>
          <el-radio-button label="我的会议"></el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        cell-style="padding: 4px 0"
        style="width: 100%;"
        size="medium"
    >
      <el-table-column width="40px" prop="description" header-align="center" align="center" type="expand">
        <template #default="scope">
          会议内容：{{ scope.row.description }}
        </template>
      </el-table-column>

      <el-table-column type="index" header-align="center" align="center" width="80" label="序号">
        <template #default="scope">
          <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="title" header-align="center" align="center" label="会议主题" min-width="300"/>
      <el-table-column prop="creatorName" header-align="center" align="center" min-width="100" label="创建者"/>
      <el-table-column prop="date" header-align="center" align="center" min-width="120" label="日期"/>
      <el-table-column header-align="center" align="center" min-width="120" label="时间">
        <template #default="scope">
          <span>{{ scope.row.start }} ~ {{ scope.row.end }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="num" header-align="center" align="center" min-width="100" label="人数"/>
      <el-table-column prop="status" header-align="center" align="center" min-width="100" label="状态"/>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template #default="scope">
          <el-button
              type="text"
              size="medium"
              :disabled="!scope.row.canEnterMeeting"
              @click="enterHandle(scope.row.id, scope.row.uuid)" dxfgn h
          >
            进入
          </el-button>
          <el-button
              type="text"
              size="medium"
              :disabled="!((scope.row.status == '待审批' || scope.row.status == '未开始') && scope.row.isCreator == 'true')"
              @click="deleteHandle(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <add v-if="addVisible" ref="add" @refresh="refresh"></add>
  </div>
</template>

<script>
import Add from './components/online_meeting-add.vue';
import dayjs from 'dayjs';

export default {
  components: {Add},
  data: function () {
    return {
      dataForm: {
        date: null,
        mold: '全部会议'
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      addVisible: false,
      dataRule: {}
    };
  },
  created: function () {
    this.loadDataList();
  },
  methods: {
    loadDataList() {
      this.dataListLoading = true;
      let data = {
        mold: this.dataForm.mold,
        page: this.pageIndex,
        length: this.pageSize
      }
      if (this.dataForm.date != null && this.dataForm.date != '') {
        data.date = dayjs(this.dataForm.date).format('YYYY-MM-DD');
      }

      this.$http("meeting/searchOnlineMeetingByPage", "POST", data, true, resp => {
        if (resp.code === 200) {
          let page = resp.page;
          for (const item of page.list) {
            if (item.status === 1) {
              item.status = '待审批';
            } else if (item.status === 3) {
              item.status = '未开始';
            } else if (item.status === 4) {
              item.status = '进行中';
            } else if (item.status === 5) {
              item.status = '已结束';
            }

            // 计算会议是否可以进入
            // 1、开会前15分钟可以进入会议室
            // 2、会议状态必须是未开始或者进行中
            let minute = dayjs(new Date()).diff(dayjs(`${item.date} ${item.start}`), 'minute');
            console.log(minute, 'minute')
            if (item.mine === 'true' && ((minute >= -10 && minute <= 0 && item.status === '未开始') || item.status === '进行中')) {
              item.canEnterMeeting = true;
            } else {
              item.canEnterMeeting = false;
            }
          }
          this.dataList = page.list;
          this.totalCount = page.totalCount;
          this.dataListLoading = false;
        }
      })
    },
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.loadDataList();
    },
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.loadDataList();
    },
    searchHandle() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$refs['dataForm'].clearValidate();
          if (this.dataForm.name === '') {
            this.dataForm.name = null;
          }
          if (this.pageIndex != 1) {
            this.pageIndex = 1;
          }
          this.loadDataList();
        } else {
          return false;
        }
      })
    },
    addHandle() {
      this.addVisible = true;
      this.$nextTick(() => {
        this.$refs.add.init();
      })
    },
    refresh() {
      this.$refs['dataForm'].resetFields();
      this.loadDataList();
    },
    deleteHandle(row) {
      this.$confirm("是否删除该会议", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let loading = this.$loading({
          lock: true,
          text: '正在删除,请稍后',
          springgreen: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.5)'
        });
        let data = {
          id: row.id,
          uuid: row.uuid,
          flowId: row.instanceId,
        }
        this.$http("meeting/deleteMeetingApplication", "DELETE", data, true, resp => {
          if (resp.rows === 1) {
            loading.close();
            this.$message({
              message: "删除会议成功",
              type: 'success',
              duration: 1500
            })
            this.searchHandle();
          } else {
            loading.close();
            this.$message({
              message: "删除会议失败",
              type: 'error',
              duration: 1000
            })
          }
        })
      })
    },
    enterHandle(id, uuid) {
      this.$router.push({
        name: 'MeetingVideo',
        params: {
          meetingId: id,
          uuid
        }
      })
    }
  }
};
</script>

<style lang="less" scoped="scoped">
@import url('../../style/online_meeting.less');
</style>
